﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.Reporting.WebForms;
using Ewts.Code;

namespace Ewts.RDLC
{
    public partial class WRLInfoRDLC : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string wrlid = "";
 
                if (Request["wrlid"] != null && Request["wrlid"].ToString().Trim() != "")
                {
                    wrlid = Request["wrlid"].ToString().Trim();
                }

                DataBindDrop(wrlid);
                dataBind(null, wrlid);

 
            }
        }

        public void DataBindDrop(string wrlid)
        {
            // 给DropDownList1绑定日期  -----肖登科 2013-5-24
            DataSet ds = new DataLib.BLL.BarCode().GetList("InStrorageId='" + wrlid + "'");
            List<string> lt = new List<string>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {

                if (lt.Contains(Convert.ToDateTime(ds.Tables[0].Rows[i]["createdate"]).ToShortDateString()))
                {
                    continue;
                }
                else
                {
                    string date = Convert.ToDateTime(ds.Tables[0].Rows[i]["createdate"]).ToShortDateString();
                    lt.Add(date);
                    dropDate.Items.Add(date);
                }

            }
            dropDate.Items.Add(new ListItem("--全选--","0"));
            dropDate.SelectedValue = "0";
            
        }

        /// <summary>
        /// 报表的数据源绑定
        /// </summary>
        /// <param name="datetime"></param>
        /// <param name="wrlid"></param>
        protected void dataBind(string datetime,string wrlid)
        {

            string cjono = "";
            string loadno = "";
            string cum = "";
            string wrldate = "";

            DataLib.Model.InStorage wrlModel = new DataLib.BLL.InStorage().GetModel(int.Parse(wrlid));
            wrldate = ((DateTime)wrlModel.instoragetime).ToLongDateString();
            DataLib.Model.LoadNum loadModel = null;
            DataLib.Model.Cjo cmodel = null;
            loadno = wrlModel.vloadnum;
            if (wrlModel.cjoid > 0)
            {
                cmodel = new DataLib.BLL.Cjo().GetModel((int)wrlModel.cjoid);
                
            }
            if( loadno.Substring(0,2) =="86")
            {

                loadModel = new DataLib.BLL.LoadNum().GetLoadID(loadno);
                wrlModel.loadnumid = loadModel.id;
            }

            loadModel = new DataLib.BLL.LoadNum().GetModel((int)wrlModel.loadnumid);
            
            DataLib.Model.OutStorage oModel = new DataLib.BLL.OutStorage().GetModel((int)wrlModel.outid);
            if(loadModel != null )
            {
                if(loadModel.printname != null && loadModel.printname.Trim()!="")
                    cum = loadModel.printname;
                else
                    cum = loadModel.cumname;
                loadno = loadModel.loadnum;
                if (wrlModel.cjoid != 0)
                {
                    cjono = cmodel.cjono;
                }
            }
            else if(oModel !=null)
            {
                loadno = oModel.VLoadNum;
                DataLib.Model.Vload vlmodel = new DataLib.BLL.Vload().GetModelByOutID((int)oModel.id);
                if(vlmodel != null)
                {
                    cum = vlmodel.inscumname;
                }
                cjono = oModel.outStorageNo;
            }
            
            List<ReportParameter> parameterList = new List<ReportParameter>();
            DataTable barCodeDS = new DataLib.BLL.InStorage().GetWrlItems(wrlid,datetime);
            //参数数据
            parameterList.Add(new ReportParameter("LoadNo", loadno));
            if (datetime != null)
            {
                parameterList.Add(new ReportParameter("CollectDate", datetime));
            }
            else
            {
                parameterList.Add(new ReportParameter("CollectDate", wrldate));
            }
            parameterList.Add(new ReportParameter("CJO", cjono));
            parameterList.Add(new ReportParameter("CustomerName", cum));
            parameterList.Add(new ReportParameter("WrlNo", wrlModel.wrlno));

            //为列表绑定数据
            ReportDataSource rds = new ReportDataSource("DataSet1", barCodeDS);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rds);
            new RDLCPrinter().FillRow(barCodeDS, 3, 15, 15);//为列表添加空行
            ReportViewer1.LocalReport.SetParameters(parameterList);
            ReportViewer1.LocalReport.Refresh();

            
        }


        //给DropDownList1添加选中项改变事件  -----肖登科 2013-5-24
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string wrlid = Request["wrlid"].ToString().Trim();
            if(dropDate.SelectedValue=="0")
                dataBind(null, wrlid);
            else
                dataBind(dropDate.SelectedValue, wrlid);

        }

       
    }
}